package com.yangdi.demo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class JoystickView extends View {
    private Paint circlePaint;
    private Paint handlePaint;
    private PointF center;
    private PointF handlePosition;
    private float radius;
    private float handleRadius;
    private JoystickListener listener;

    public JoystickView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        circlePaint = new Paint();
        circlePaint.setColor(Color.GRAY);
        circlePaint.setStyle(Paint.Style.FILL);

        handlePaint = new Paint();
        handlePaint.setColor(Color.BLUE);
        handlePaint.setStyle(Paint.Style.FILL);

        center = new PointF();
        handlePosition = new PointF();
        radius = 100;
        handleRadius = 40;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        center.x = w / 2f;
        center.y = h / 2f;
        handlePosition.set(center);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawCircle(center.x, center.y, radius, circlePaint);
        canvas.drawCircle(handlePosition.x, handlePosition.y, handleRadius, handlePaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                float touchX = event.getX();
                float touchY = event.getY();
                float dx = touchX - center.x;
                float dy = touchY - center.y;
                float distance = (float) Math.sqrt(dx * dx + dy * dy);

                if (distance <= radius) {
                    handlePosition.set(touchX, touchY);
                } else {
                    float ratio = radius / distance;
                    handlePosition.set(center.x + dx * ratio, center.y + dy * ratio);
                }

                if (listener != null) {
                    float normalizedX = (handlePosition.x - center.x) / radius;
                    float normalizedY = (handlePosition.y - center.y) / radius;
                    listener.onJoystickMoved(normalizedX, normalizedY);
                }
                invalidate();
                return true;
            case MotionEvent.ACTION_UP:
                handlePosition.set(center);
                if (listener != null) {
                    listener.onJoystickMoved(0, 0);
                }
                invalidate();
                return true;
        }
        return super.onTouchEvent(event);
    }

    public void setJoystickListener(JoystickListener listener) {
        this.listener = listener;
    }

    public interface JoystickListener {
        void onJoystickMoved(float x, float y);
    }}